package cn.cyh.order.service.impl;

import cn.cyh.basic.service.impl.BaseServiceImpl;
import cn.cyh.basic.uitls.CodeGenerateUtils;
import cn.cyh.basic.uitls.PageList;
import cn.cyh.order.constants.OrderConstans;
import cn.cyh.order.domain.AdoptOrder;
import cn.cyh.order.domain.OrderAddress;
import cn.cyh.order.mapper.AdoptOrderMapper;
import cn.cyh.order.mapper.OrderAddressMapper;
import cn.cyh.order.query.AdoptOrderQuery;
import cn.cyh.order.service.IAdoptOrderService;
import cn.cyh.org.domain.Employee;
import cn.cyh.org.mapper.EmployeeMapper;
import cn.cyh.pay.constants.PayConstants;
import cn.cyh.pay.domain.PayBill;
import cn.cyh.pay.mapper.PayBillMapper;
import cn.cyh.pay.service.IPayBillService;
import cn.cyh.pet.domain.Pet;
import cn.cyh.pet.mapper.PetMapper;
import cn.cyh.user.domain.LoginInfo;
import cn.cyh.user.domain.User;
import cn.cyh.user.domain.UserAddress;
import cn.cyh.user.mapper.UserAddressMapper;
import cn.cyh.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
public class AdoptOrderServiceImpl extends BaseServiceImpl<AdoptOrder> implements IAdoptOrderService {
    @Autowired
    private PetMapper petMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private AdoptOrderMapper adoptOrderMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private IPayBillService payBillService;


    @Override
    @Transactional
    public String sumbit(Map<String, Object> params, LoginInfo loginInfo) {
        // 接收参数和参数校验
        // 送货方式
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());
        // 收货地址id
        Long addressId = Long.valueOf(params.get("address_id").toString());
        // 支付方式 1是支付宝 2是微信 0是银联
        Long payMethed = Long.valueOf(params.get("pay_method").toString());
        // 宠物id
        Long petId = Long.valueOf(params.get("pet_id").toString());
        // 修改宠物状态+下架+绑定领养人
        // 下架
        // 绑定用户
        Pet pet = petMapper.loadById(petId);
        User user = userMapper.loadByLoginInfoId(loginInfo.getId());

        pet.setUser_id(user.getId());
        pet.setOffsaletime(new Date());
        petMapper.update(pet);
        // 创建领养订单
            // 订单本表
        AdoptOrder order = getAdoptOrder(pet, user);
        // 为了后面方便取值 生成支付单不用修改 所以先直接生成统一支付标识
        order.setPaySn(CodeGenerateUtils.generateUnionPaySn());
        adoptOrderMapper.save(order); // 返回自增id
            // 订单地址表
        UserAddress userAddress = userAddressMapper.loadById(addressId);
        OrderAddress orderAddress = initOrderAddress(order, userAddress);
        orderAddressMapper.save(orderAddress);
        // 支付订单
        PayBill payBill = getPayBill(payMethed, user, order);
        payBillMapper.save(payBill);
        // 统一支付接口
        return payBillService.pay(payBill);
    }



    @Override
    public PageList<AdoptOrder> admin(AdoptOrderQuery query, LoginInfo loginInfo) {
        Employee employee = employeeMapper.loadByLoginInfoId(loginInfo.getId());
        if (employee.getShop_id()!=null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public PageList<AdoptOrder> user(AdoptOrderQuery query, LoginInfo loginInfo) {
        User user = userMapper.loadByLoginInfoId(loginInfo.getId());
        query.setUserId(user.getId());

        return super.queryPage(query);
    }

    @Override
    public void deleteByQuartz(Long orderId) {
        AdoptOrder adoptOrder = adoptOrderMapper.loadById(orderId);
        if(adoptOrder.getState() ==0){//这里可能有隐藏的bug
            adoptOrder.setState(-1);
            // 这里可以考虑直接上架宠物  @TODO
            adoptOrderMapper.update(adoptOrder);
            System.out.println("quartz 触发直接取消订单："+orderId);
        }

    }

    private OrderAddress initOrderAddress(AdoptOrder order, UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress,orderAddress);
        orderAddress.setOrder_id(order.getId());
        orderAddress.setOrderSn(order.getOrderSn());
        return orderAddress;
    }

    private AdoptOrder getAdoptOrder(Pet pet, User user) {
        AdoptOrder order = new AdoptOrder();
        order.setDigest("[摘要]对"+pet.getName()+"领养订单！");
        order.setPrice(pet.getSaleprice());
        String orderSn = CodeGenerateUtils.generateOrderSn(user.getId());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(user.getId());
        order.setShop_id(pet.getShop_id());
        order.setLastPayTime(new Date(System.currentTimeMillis()+ OrderConstans.LSAT_PAY_TIME));
        return order;
    }
    private PayBill getPayBill(Long payMethed, User user, AdoptOrder order) {
        PayBill payBill = new PayBill();
        payBill.setDigest(order.getDigest());
        payBill.setMoney(order.getPrice());
        payBill.setUnionPaySn(order.getPaySn());
        payBill.setLastPayTime(new Date(System.currentTimeMillis()+ OrderConstans.LSAT_PAY_TIME));
        payBill.setPayChannel(payMethed);
        // 领养订单类型
        payBill.setBusinessType(PayConstants.BUSINESS_TYPE_ADOPT);
        payBill.setBusinessKey(order.getId());
        payBill.setUser_id(order.getUser_id());
        payBill.setShop_id(order.getShop_id());
        payBill.setNickName(user.getUsername());
        return payBill;
    }
}
